//html模板标签
const fs = require('fs');
const shuJuKu = require('./shuJuKu.js');

async function muBanYinQing(leiXing, hao) {
	let [sheZhi] = await shuJuKu.chaSheZhi();
	let muBan = '';
	let dangQianLanMu = {};
	let dangQianWenZhang = {};
	let wHao, lHao,
		fHao = 0,
		yeHao = 1, //页号
		yeShu = 1, //分页页数
		fenYeHao = 0; //分页条划分号
	//判断页面类型
	if (leiXing == 'shouYe') {
		lHao = 0;
		muBan = 'shouYe.html'
	}
	if (leiXing == 'lanMu' && hao) {
		let lanMuHao = hao.match(/^(\d+)$/);
		let lanMuLieBiaoHao = hao.match(/^(\d+)-(\d+)$/);
		if (lanMuHao) {
			lHao = lanMuHao[1];
		} else if (lanMuLieBiaoHao) {
			lHao = lanMuLieBiaoHao[1];
			yeHao = lanMuLieBiaoHao[2];
		} else {
			return 404;
		}
		if (yeHao <= 0) yeHao = 1;
		[dangQianLanMu] = await shuJuKu.chaLanMuDan(lHao);
		if (!dangQianLanMu) return 404;
		muBan = dangQianLanMu.muBan
	}
	if (leiXing == 'wenZhang' && hao) {
		wHao = hao;
		[dangQianWenZhang] = await shuJuKu.chaWenZhangDan(wHao);
		if (!dangQianWenZhang) return 404;
		lHao = dangQianWenZhang.lHao;
		[dangQianLanMu] = await shuJuKu.chaLanMuDan(lHao);
		muBan = dangQianWenZhang.muBan
	}
	//读取模板
	let muBanZiFuChuan = fs.readFileSync('./muBan/' + muBan).toString();
	let biaoQian;
	//标签替换内容
	muBanZiFuChuan = muBanZiFuChuan.replace(/\{syt:biaoTi\}/g, sheZhi.biaoTi);
	muBanZiFuChuan = muBanZiFuChuan.replace(/\{syt:lHao\}/g, dangQianLanMu.lHao);
	muBanZiFuChuan = muBanZiFuChuan.replace(/\{syt:biaoTi1\}/g, dangQianLanMu.biaoTi);
	muBanZiFuChuan = muBanZiFuChuan.replace(/\{syt:neiRong1\}/g, dangQianLanMu.neiRong);
	muBanZiFuChuan = muBanZiFuChuan.replace(/\{syt:biaoTi2\}/g, dangQianWenZhang.biaoTi);
	muBanZiFuChuan = muBanZiFuChuan.replace(/\{syt:neiRong2\}/g, dangQianWenZhang.neiRong);
	muBanZiFuChuan = muBanZiFuChuan.replace(/\{syt:riQi\}/g, dangQianWenZhang.riQi);
	//指定栏目
	if (biaoQian = muBanZiFuChuan.match(/\{syt:lanMu[\w\W]*?syt:lanMu\}/g)) {
		for (let hm of biaoQian) {
			let lanMu;
			let lHao;
			if (hm.match(/lHao=(\d+)/)) lHao = hm.match(/lHao=(\d+)/)[1];
			else continue;
			[lanMu] = await shuJuKu.chaLanMuDan(lHao);
			if (lanMu) {
				let tuPian = !lanMu.neiRong.match(/<img src="(.*?)"/) || lanMu.neiRong.match(
					/<img src="(.*?)"/)[1];
				let muBanNeiRong = hm.match(/\{syt:lanMu[\w\W]*?\}([\w\W]*?)\{\/syt:lanMu\}/)[1];
				muBanNeiRong = muBanNeiRong.replace(/\[lanMu:biaoTi\]/g, lanMu.biaoTi);
				muBanNeiRong = muBanNeiRong.replace(/\[lanMu:neiRong\]/g, lanMu.neiRong);
				muBanNeiRong = muBanNeiRong.replace(/\[lanMu:neiRong chang=(\d+)\]/g, (match, p1) => lanMu.neiRong.replace(
					/<.*?>/g, '').substring(0, p1));
				muBanNeiRong = muBanNeiRong.replace(/\[lanMu:lianJie\]/g, `/lanMu-${lanMu.lHao}`);
				muBanNeiRong = muBanNeiRong.replace(/\[lanMu:tuPian\]/g, tuPian);
				muBanZiFuChuan = muBanZiFuChuan.replace(hm, muBanNeiRong);
			}
		}
	}
	
	//导航栏目列表
	let lanMuShuJu = await shuJuKu.chaLanMu();
	lanMuShuJu.forEach((zhi, jian, dui) => {
		dui.forEach(x => {
			if (zhi.fHao != 0 && x.lHao == zhi.fHao) {
				x.zi ? x.zi : x.zi = [];
				zhi.chongFu = true;
				x.zi.push(zhi);
			}
		})
	})
	lanMuShuJu = lanMuShuJu.filter( x => !x.chongFu)
	function diGui(p, lanMuShuZu) {
		if (p == 0) {
			return lanMuShuZu;
		}else {
			for (let lanMu of lanMuShuZu) {
				if (p == lanMu.lHao){
					if (lanMu.zi) return lanMu.zi;
				}else if (lanMu.zi) diGui(p, lanMu.zi);
			}
		}
	}
	
	function tiHuan(ziFuChuan, cengJi = 0) {
		let daoHangCengJi = cengJi == 0 ? '' : cengJi;
		let reg = new RegExp(`{syt:daoHang${daoHangCengJi}.*?syt:daoHang${daoHangCengJi}}`, 'gs')
		if (biaoQian = ziFuChuan.match(reg)) {
			for (let hm of biaoQian) {
				let daoHangShuZu;
				let shu;
				if (hm.match(/fHao=(\d+)/)) fHao = hm.match(/fHao=(\d+)/)[1];
				else continue;
				if (hm.match(/shu=(\d+)/)) shu = hm.match(/shu=(\d+)/)[1];
				//获取栏目数组
				let lanMuJieGuo = diGui(fHao, lanMuShuJu)
				if (!lanMuJieGuo)  {
					ziFuChuan = ziFuChuan.replace(reg, '');
					continue;
				};
				daoHangShuZu = lanMuJieGuo.slice(0, shu);
				let reg1 = new RegExp(`{syt:daoHang${daoHangCengJi}.*?}(.*?){/syt:daoHang${daoHangCengJi}}`, 's');
				let muBanNeiRong = hm.match(reg1)[1];
				
				muBanNeiRong = daoHangShuZu.reduce((zongJi, zhi, j, s) => {
					let tuPian = !zhi.neiRong.match(/<img src="(.*?)"/) || zhi.neiRong.match(/<img src="(.*?)"/)[1];
					
					let reg2 = new RegExp(`\\[daoHang${daoHangCengJi}:biaoTi\\]`, 'g');
					zongJi += muBanNeiRong.replace(reg2, zhi.biaoTi);
					let reg3 = new RegExp(`\\[daoHang${daoHangCengJi}:lianJie\\]`, 'g');
					zongJi = zongJi.replace(reg3, `/lanMu-${zhi.lHao}`);
					let reg4 = new RegExp(`\\[daoHang${daoHangCengJi}:neiRong\\]`, 'g');
					zongJi = zongJi.replace(reg4, zhi.neiRong);
					let reg5 = new RegExp(`\\[daoHang${daoHangCengJi}:neiRong chang=(\\d+)\\]`, 'g');
					zongJi = zongJi.replace(reg5, (match, p1) => zhi.neiRong.replace(
						/<.*?>/g, '').substring(0, p1));
					let reg6 = new RegExp(`\\[daoHang${daoHangCengJi}:tuPian\\]`, 'g');
					zongJi = zongJi.replace(reg6, tuPian);
					let reg7 = new RegExp(`\\[daoHang${daoHangCengJi}:lHao\\]`, 'g');
					zongJi = zongJi.replace(reg7, zhi.lHao);

					//多级递归
					let reg = new RegExp(`{syt:daoHang${+daoHangCengJi + 1}.*?syt:daoHang${+daoHangCengJi + 1}}`, 'gs')
					if (zongJi.match(reg)) {
						zongJi =  tiHuan(zongJi, +daoHangCengJi + 1);
					}
					return zongJi;
				}, '')
				ziFuChuan = ziFuChuan.replace(hm, muBanNeiRong);
			}
		}
		return ziFuChuan;
	}
	muBanZiFuChuan = tiHuan(muBanZiFuChuan);
	
	//指定文章
	if (biaoQian = muBanZiFuChuan.match(/\{syt:wenZhang[\w\W]*?syt:wenZhang\}/g)) {
		for (let hm of biaoQian) {
			let wenZhangShuJu;
			let wHao;
			if (hm.match(/wHao=(\d+)/)) wHao = hm.match(/wHao=(\d+)/)[1];
			else continue;
			[wenZhangShuJu] = await shuJuKu.chaWenZhangDan(wHao);
			if (wenZhangShuJu) {
				let tuPian = !wenZhangShuJu.neiRong.match(/<img src="(.*?)"/) || wenZhangShuJu.neiRong.match(
					/<img src="(.*?)"/)[1];
				let muBanNeiRong = hm.match(/\{syt:wenZhang[\w\W]*?\}([\w\W]*?)\{\/syt:wenZhang\}/)[1];
				muBanNeiRong = muBanNeiRong.replace(/\[wenZhang:biaoTi\]/g, wenZhangShuJu.biaoTi);
				muBanNeiRong = muBanNeiRong.replace(/\[wenZhang:neiRong\]/g, wenZhangShuJu.neiRong);
				muBanNeiRong = muBanNeiRong.replace(/\[wenZhang:neiRong chang=(\d+)\]/g, (match, p1) => wenZhangShuJu.neiRong.replace(
					/<.*?>/g, '').substring(0, p1));
				muBanNeiRong = muBanNeiRong.replace(/\[wenZhang:tuPian\]/g, tuPian);
				muBanNeiRong = muBanNeiRong.replace(/\[wenZhang:lianJie\]/g, `/wenZhang-${wenZhangShuJu.wHao}`);
				muBanZiFuChuan = muBanZiFuChuan.replace(hm, muBanNeiRong);
			}
		}
	}
	//文章列表
	if (biaoQian = muBanZiFuChuan.match(/\{syt:lieBiao[\w\W]*?syt:lieBiao\}/g)) {
		for (let hm of biaoQian) {
			let wenZhangLieBiao;
			let shu;
			if (hm.match(/shu=(\d+)/)) shu = hm.match(/shu=(\d+)/)[1];
			if (leiXing == 'lanMu' && !hm.match(/lHao=(\d+)/) && hm.match(/fenYe/)) {
				//当前栏目内容分页
				wenZhangLieBiao = await shuJuKu.chaWenZhang();
				wenZhangLieBiao = wenZhangLieBiao.filter(x => {
					let jieGuo;
					if (lHao === 0) jieGuo = true;
					if (x.lHao == lHao) jieGuo = true;
					function diGui(l) {
						if (x.lHao == l.lHao) {
							jieGuo = true;
							return;
						}
						if (l.zi) {
							l.zi.forEach(z => diGui(z))
						}
					}
					for (let d of lanMuShuJu) {
						if (d.lHao == lHao) {
							diGui(d);
							break;
						}
					}
					return jieGuo;
				})
				
				
				yeShu = Math.ceil(wenZhangLieBiao.length / shu);
				if (yeShu == 0) yeShu = 1;
				if (yeHao > yeShu) {
					return 404;
				}
				wenZhangLieBiao = wenZhangLieBiao.slice((yeHao - 1) * shu, yeHao * shu)
			} else {
				//指定栏目内容不分页
				let l = lHao;
				if (hm.match(/lHao=(\d+)/)) l = hm.match(/lHao=(\d+)/)[1];
				wenZhangLieBiao = await shuJuKu.chaWenZhangDuo(l, shu);
			}
			let muBanNeiRong = hm.match(/\{syt:lieBiao[\w\W]*?\}([\w\W]*?)\{\/syt:lieBiao\}/)[1];
			muBanNeiRong = wenZhangLieBiao.reduce((zongJi, zhi, j, dui) => {
				let tuPian = !zhi.neiRong.match(/<img src="(.*?)"/) || zhi.neiRong.match(/<img src="(.*?)"/)[1];
				zongJi += muBanNeiRong.replace(/\[lieBiao:biaoTi\]/g, zhi.biaoTi);
				zongJi = zongJi.replace(/\[lieBiao:neiRong\]/g, zhi.neiRong);
				zongJi = zongJi.replace(/\[lieBiao:neiRong chang=(\d+)\]/g, (match, p1) => zhi.neiRong.replace(
					/<.*?>/g, '').substring(0, p1));
				zongJi = zongJi.replace(/\[lieBiao:riQi\]/g, zhi.riQi);
				zongJi = zongJi.replace(/\[lieBiao:tuPian\]/g, tuPian);
				zongJi = zongJi.replace(/\[lieBiao:lianJie\]/g, `/wenZhang-${zhi.wHao}`);
				return zongJi;
			}, '')
			muBanZiFuChuan = muBanZiFuChuan.replace(hm, muBanNeiRong);
		}
	}
	//分页链接
	if (leiXing == 'lanMu' && (biaoQian = muBanZiFuChuan.match(/{syt:fenYeTiao}/))) {
		let shangYe = `<a href="/lanMu-${lHao}-${+yeHao-1}">上一页</a>`;
		let xiaYe = `<a href="/lanMu-${lHao}-${+yeHao+1}">下一页</a>`;
		let fenYeShuZu = [];
		for (let i = 0; i < Math.ceil(yeShu / 5); i++) {
			let fenYeZiFuChuan = '';
			for (let j = 1; j <= 5; j++) {
				if ((i * 5) + j <= yeShu) {
					if ((i * 5) + j == yeHao) fenYeZiFuChuan +=
						`<a class="dang-qian-ye" href="/lanMu-${lHao}-${(i*5)+j}">${(i*5)+j}</a>`;
					else fenYeZiFuChuan += `<a href="/lanMu-${lHao}-${(i*5)+j}">${(i*5)+j}</a>`;
				}
			}
			fenYeShuZu.push(fenYeZiFuChuan)
		}
		if (yeHao == 1) {
			shangYe = `<a>上一页</a>`
		}
		if (yeHao == yeShu) {
			xiaYe = `<a>下一页</a>`
		}
		fenYeShuZu.forEach((zhi, jian) => {
			if (zhi.includes(`-${yeHao}"`)) fenYeHao = jian;
		})
		muBanZiFuChuan = muBanZiFuChuan.replace(/{syt:fenYeTiao}/,
			`<a href="/lanMu-${lHao}-1">首页</a>	
			${shangYe}
			${fenYeShuZu[fenYeHao]}
			${xiaYe}
			<a href="/lanMu-${lHao}-${yeShu}">尾页</a>
			`
		)
	}
	return muBanZiFuChuan
}

module.exports = muBanYinQing;
